// import sqlite data into mongodb

let path = require('path');
let co = require('co');
let Promise = require('bluebird');
let Database = require('better-sqlite3');

let sqliteDBPath = path.resolve(__dirname, './database.db');

let sqliteDatabase = new Database(sqliteDBPath);

// connect mongodb
require('./models');

let statement = sqliteDatabase.prepare('SELECT * FROM items');
let iterator = statement.iterate();

let pos = 0;
function rowRecursive(iterator) {
    let row = iterator.next();
    if (!row) {
        // err
        console.log('err');
        return;
    }
    if (row.done) {
        // complete
        console.log('complete');
        console.log(new Date().getTime());
        return;
    }
    let book = JSON.parse(row.value.BookInfo);
    book.bookNum = row.value.BookNum;
    book.id = row.value.Id;

    // book process
    co.wrap(bookProcessGen)(book)
        .then(function () {
            // console.log(book);
            pos++;
            console.log(pos);
            rowRecursive(iterator);
        })
        .catch(function (err) {
            console.log(book);
            console.log(JSON.stringify(err));
        });
}

console.log(new Date().getTime());
rowRecursive(iterator);

// sqliteDatabase.each('SELECT * FROM items', function (err, row) {
//
//     if (err) {
//         console.log(JSON.stringify(err));
//         return;
//     }
//     // console.log(JSON.stringify(row));
//
//     let book = JSON.parse(row.BookInfo);
//     book.bookNum = row.BookNum;
//     book.id = row.id;
//
//     // book process
//     co.wrap(bookProcessGen)(book)
//         .then(function () {
//             console.log(book);
//         })
//         .catch(function (err) {
//             console.log(JSON.stringify(err));
//         });
// }, function () {
//     console.log('complete');
//     sqliteDatabase.close();
// });

// book process
function* bookProcessGen(book) {
    // find book
    let isbn = book.isbn10 || book.isbn13;
    let bookResult = yield findBook(isbn);

    if (bookResult) {
        // book exist, update book
        let updateResult = yield updateBook(book);
        // console.log(updateResult);
    } else {
        // book not exist
        let newBook = yield createBook(book);
        // console.log(newBook.toJSON());
    }
    // let newBook = yield createBook(book);
}

function findBook(isbn) {
    return new Promise(function (resolve, reject) {
        if (!isbn) {
            reject();
            return;
        }

        // Book.findOne({$or: [{isbn10: isbn}, {isbn13: isbn}]}, function (err, bookResult) {
        Book.findOne({$or: [{isbn10: isbn}, {isbn13: isbn}]}).exec(function (err, bookResult) {
            if (err) {
                reject(err);
                return;
            }
            resolve(bookResult);
            return;
        });
    });
}

// book update
function updateBook(book) {
    return new Promise(function (resolve, reject) {

        if (!book) {
            reject();
            return;
        }
        let isbn = book.isbn10 || book.isbn13;
        Book.update({$or: [{isbn10: isbn}, {isbn13: isbn}]}, book, {multi: true}, function (err, updateResult) {
            if (err) {
                reject(err);
                return;
            }
            resolve(updateResult);
            return;
        });
    });
}

// create book
function createBook(book) {
    return new Promise(function (resolve, reject) {

        if (!book) {
            reject();
            return;
        }

        Book.create(book, function (err, bookResult) {
            if (err) {
                reject(err);
                return;
            }
            resolve(bookResult);
            return;
        });
    });
}


